There has been a lot of confusion about
global and local variables lately so I thought I would create some
visuals to help everyone understand.
When one starts Macro Express think of the variable space as one
blank whiteboard with no boundaries and no variables defined. When we
create macros and define variables we need fences and corrals to keep
them penned in so I will create representative circles.
Before we go too far let me create some rules.
- Each macro is given it's own space when it starts. This is
absolute and no other macros can access this space.
- Each macro that is called by parent uses the same global space
but has it's own local space.
- All macros use their own local variable before the global. IE
they will only use the global value when the local does not exist.
- All variables created by a macro cease to exist when the macro
is done.
So here is what I envision when I launch a macro with some sample
variables. Call it A.
Now if I launch anther macro B that is not run from A it might look
like this:
But since they all have their own space we can ignore that in the
paradigm.
Now let's use Macro Run to launch macro B from A.
Now things get interesting. Now you can see that the global space is
shared. In this case the %Counter% in A can be 1 and the counter in B
can b 2. But if you use %File Name% in either they share the same var.
Here we have a case where %SSN% is defined as a local in B so when
it's called by A an SSN like 123-45-6789 will be stored to the local
value. In fact there is no way to store it to the global. And if one
uses %SSN% in B it will look to the local B value first.
Now with all this don't forget that one is not required to define a
variable like %File Name% in B. You might get a warning box while
creating B that it is not defined but that's OK, go ahead and dismiss
the warning. However I think it's a good practice to define it as a
global as well if for no other reason to keep track of your vars.
Now I have to give props to Joe Weinpert here. We had a debate the
other night about whether or not they are truly global vars and I have
to admit I was wrong, they're not. But up to this point for all
practical purposes they are. So how are they not? Take a look at the
'global' checkbox label:
This is actually the most accurate description of how it works. An
example of how these are not truly global is this... Create 3 macros,
Parent, Sub1 and Sub2. Define variable %Test% in Sub1 and create one
like to set %Test% to "Something" then perform a basic pause. Now in
Sub2 insert a pause and create a text box that displays %Test%. In
Parent run Sub1 but choose not to wait for the the called macro to
terminate before proceeding. When you run A it sets the var %Test% but
as it awaits closure Sub2 runs and displays %Test%. In this example it
will not correctly show "Something" meaning it is not truly a global var
but rather doing exactly as the check box label suggests.